﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Drawing;
using System.Collections.Generic;

public partial class admin_ExportListHocVienTVTS : System.Web.UI.UserControl
{
    #region "Member variables"
    private int intUserID = 0;
    private int intModuleID = 0;
    private int intUserList = 0;
    int intNhom = 0;
    int intChucNangID = 0;
    int intLevelID = 0;
    bool boolLevel2345678 = false;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DALevel objLevel;
    DANguon objNguon;
    DAQuyen objQuyen;
    DAHocVienLV2 objHocVienLV2;
    DAHocVienLV3LV5 objHocVienLV3LV5;
    DAHocVienLV6LV8 objHocVienLV6LV8;
    DAHocVien objHocVien;
    DANguoidung objNguoidung;
    DataView m_dv_export_2_data = new DataView();
    #endregion
    #region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion
    #region "Private methods"
    private void loadNguon()
    {
        DataSet dsNguon = objNguon.LayDanhSachNguon();
        m_cbO_nguon.Items.Clear();
        m_cbO_nguon.Items.Add(new ListItem("Tất cả các nguồn", "0"));
        for (int v_i = 0; v_i < dsNguon.Tables[0].Rows.Count; v_i++)
        {
            m_cbO_nguon.Items.Add(new ListItem(dsNguon.Tables[0].Rows[v_i]["Ten"].ToString(), dsNguon.Tables[0].Rows[v_i]["ID"].ToString()));
        }
    }
    private void loadTVTS()
    {
        DataSet dsTVTS = objNguoidung.load_tu_van_tuyen_sinh();
        m_cbo_tvts.Items.Clear();
        m_cbo_tvts.Items.Add(new ListItem("------ Hãy chọn 01 TVTS ------", "0"));
        for (int v_i = 0; v_i < dsTVTS.Tables[0].Rows.Count; v_i++)
        {
            m_cbo_tvts.Items.Add(new ListItem(dsTVTS.Tables[0].Rows[v_i]["HoTen"].ToString(), dsTVTS.Tables[0].Rows[v_i]["ID"].ToString()));
        }
    }
    /// <summary>
    ///  /// Created by LinhDH
    /// Hàm này dùng để kiểm tra người dùng đăng nhập. 
    /// Nếu nằm trong danh sách cấm, không được xuất excel thì invisible nút xuất excel
    /// </summary>
    /// <returns>true nếu người dùng bị cấm; false nếu ng dùng ko bị cấm</returns>
    private bool check_quyen_xuat_excel()
    {
        string v_str_duong_dan_file;
        int v_i_flag = 0;
        v_str_duong_dan_file = Server.MapPath("~/UserControl/HocVien/DS_tai_khoan_dc_xuat_contact.txt");
        string[] v_str_danh_sach_nguoi_dung_cam = File.ReadAllLines(v_str_duong_dan_file);
        string v_str_account = Session["Username"].ToString();
        for (int v_i = 0; v_i < v_str_danh_sach_nguoi_dung_cam.Length; v_i++)
        {
            if (v_str_danh_sach_nguoi_dung_cam[v_i].Equals(v_str_account))
            {
                v_i_flag = 1;
                break;
            }
        }
        if (v_i_flag > 0) return true;  //đc xuất
        return false; // User bị cấm
    }

    private void loadTimDanhSachHocVien()
    {
        try
        {
            int v_i_tvts = Convert.ToInt32(m_cbo_tvts.SelectedValue);
            if (v_i_tvts == 0)
            {
                string v_str_script = "<script language='javascript'>alert('Hãy chọn một TVTS !')</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "oncheckTVTS", v_str_script);
                return;
            }
            int v_i_nguon_id = Convert.ToInt32(m_cbO_nguon.SelectedValue);

            string v_str_level = "";
            string v_str_trang_thai_goi = "";
            for (int v_i = 0; v_i < m_cbl_level.Items.Count; v_i++)
            {
                v_str_level += m_cbl_level.Items[v_i].Selected ? m_cbl_level.Items[v_i].Value.ToString() + "," : "";
            }
            
            for (int v_i = 0; v_i < m_cbl_trang_thai_goi.Items.Count; v_i++)
            {
                v_str_trang_thai_goi += m_cbl_trang_thai_goi.Items[v_i].Selected ? m_cbl_trang_thai_goi.Items[v_i].Value.ToString() + "," : "";
            }

            DateTime v_dat_nhap_tu_ngay, v_dat_nhap_den_ngay, v_dat_goi_tu_ngay, v_dat_goi_den_ngay;
            if (this.m_dat_nhap_tu_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_nhap_tu_ngay = this.m_dat_nhap_tu_ngay.SelectedDate;
            else v_dat_nhap_tu_ngay = Convert.ToDateTime("01/01/1900");

            if (this.m_dat_nhap_den_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_nhap_den_ngay = this.m_dat_nhap_den_ngay.SelectedDate;
            else v_dat_nhap_den_ngay = Convert.ToDateTime("01/01/1900");

            if (this.m_dat_goi_tu_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_goi_tu_ngay = this.m_dat_goi_tu_ngay.SelectedDate;
            else v_dat_goi_tu_ngay = Convert.ToDateTime("01/01/1900");

            if (this.m_dat_goi_den_ngay.SelectedDate != Convert.ToDateTime("01/01/0001"))
                v_dat_goi_den_ngay = this.m_dat_goi_den_ngay.SelectedDate;
            else v_dat_goi_den_ngay = Convert.ToDateTime("01/01/1900");

            DataView dv = new DataView();
            DataSet dsHocVien = objHocVien.GetExportTimDanhSachHocVien(v_i_tvts
                                                                    , v_str_level
                                                                    , v_str_trang_thai_goi
                                                                    , v_i_nguon_id
                                                                    , v_dat_nhap_tu_ngay
                                                                    , v_dat_nhap_den_ngay
                                                                    , v_dat_goi_tu_ngay
                                                                    , v_dat_goi_den_ngay);
            
            if (dsHocVien.Tables[0].Rows.Count > 0) btnInDanhSach.Enabled = true;
            if (ViewState["sortExpr"] != null)
            {
                dv = new DataView(dsHocVien.Tables[0]);
                dv.Sort = (string)ViewState["sortExpr"];
            }
            else
            {
                dv = new DataView(dsHocVien.Tables[0]);
            }
            this.grvHocVien.DataSource = dv;
            this.grvHocVien.DataBind();
        }
        catch(Exception v_e)
        { throw v_e; }
    }
    public void SaveExcel(string strNamFile)
    {
        ImportExportExcel.export_gridview_2_excel(grvHocVien
                                    , strNamFile);
    }
    #endregion
    #region "Events handle"
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objHocVienLV2 = new DAHocVienLV2(sqlConnec);
        objHocVienLV3LV5 = new DAHocVienLV3LV5(sqlConnec);
        objHocVienLV6LV8 = new DAHocVienLV6LV8(sqlConnec);
        objHocVien = new DAHocVien(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
        objLevel = new DALevel(sqlConnec);
        objQuyen = new DAQuyen(sqlConnec);
        objNguon = new DANguon(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
        loadDataAccess();
        intUserID = Convert.ToInt32(Session["UserID"].ToString());
        if (Request.QueryString["ChucNang"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["ChucNang"].Trim());
            if (boolCheck)
            {
                intChucNangID = Convert.ToInt32(Request.QueryString["ChucNang"].Trim());
            }
        }
        if (Request.QueryString["Nhom"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["Nhom"].Trim());
            if (boolCheck)
            {
                intNhom = Convert.ToInt32(Request.QueryString["Nhom"].Trim());
            }
        }
        if (Request.QueryString["LevelView"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["LevelView"].Trim());
            if (boolCheck)
            {
                intLevelID = Convert.ToInt32(Request.QueryString["LevelView"].Trim());
            }
        }
        if (Request.QueryString["UserList"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["UserList"].Trim());
            if (boolCheck)
            {
                intUserList = Convert.ToInt32(Request.QueryString["UserList"].Trim());
            }
        }
        if (!Page.IsPostBack)
        {
            try
            {
                loadNguon();
                loadTVTS();
                // nếu account đã bị khóa chức năng xuất excel
                if (check_quyen_xuat_excel())
                {
                    btnInDanhSach.Visible = true;
                    btnSearch.Visible = true;
                }
                else
                {
                    btnInDanhSach.Visible = false;
                    btnSearch.Visible = false;
                }
                btnInDanhSach.Enabled = false;
            }
            catch
            { }
        }
    }

    //Su kien bin from NguoiTimNguon
    //protected void grvHocVien_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        if (e.Row.DataItem != null)
    //        {
    //            e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
    //            e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
    //            // Modify     
    //            Label HocVienIDTemp = ((Label)e.Row.FindControl("HocVienID"));
    //            Label LevelIDTemp = ((Label)e.Row.FindControl("LevelID"));
    //            ImageButton lbtDeleteTemp = ((ImageButton)e.Row.FindControl("btnDelete"));
    //            if (lbtDeleteTemp != null)
    //            {
    //                bool checkQuyen = false;

    //                if (intUserID == 1)
    //                {
    //                    checkQuyen = true;
    //                }
    //                else
    //                {
    //                    DataSet dsNguoidung = objNguoidung.loadQuyenNguoidungbyNguoiDung(intUserID);
    //                    if (dsNguoidung != null && dsNguoidung.Tables[0].Rows.Count > 0)
    //                    {
    //                        foreach (DataRow dr in dsNguoidung.Tables[0].Rows)
    //                        {
    //                            if (dr["QuyenID"].ToString().Equals("1") || dr["QuyenID"].ToString().Equals("2"))
    //                            {
    //                                checkQuyen = true;
    //                                break;
    //                            }
    //                        }
    //                    }
    //                }
    //            }
    //            Label lblMaHocVienGVTemp = ((Label)e.Row.FindControl("lblMaHocVienGV"));                
    //            HyperLink hplUpdateGVTemp = ((HyperLink)e.Row.FindControl("hplUpdateGV"));
    //            HyperLink hplAddGVTemp = ((HyperLink)e.Row.FindControl("hplAddGV"));
    //            if (lblMaHocVienGVTemp != null)
    //            {
    //                string strViewMaHocVien = "JavaScript:OpenWinCenter(700, 800, '" + lblMaHocVienGVTemp.Text + "', 'EventHocVien.aspx?LevelView=" + LevelIDTemp.Text + "&HocVienID=" + HocVienIDTemp.Text + "&EventView=True');";
    //                e.Row.Cells[2].Attributes["Ondblclick"] = strViewMaHocVien;
    //                e.Row.Cells[3].Attributes["Ondblclick"] = strViewMaHocVien;
    //                e.Row.Cells[4].Attributes["Ondblclick"] = strViewMaHocVien;
    //                e.Row.Cells[5].Attributes["Ondblclick"] = strViewMaHocVien;
    //                e.Row.Cells[6].Attributes["Ondblclick"] = strViewMaHocVien;
    //                e.Row.Cells[7].Attributes["Ondblclick"] = strViewMaHocVien;
    //                e.Row.Cells[8].Attributes["Ondblclick"] = strViewMaHocVien;
    //                e.Row.Cells[9].Attributes["Ondblclick"] = strViewMaHocVien;
    //                if (boolLevel2345678)
    //                {
    //                    hplAddGVTemp.Attributes.Add("OnClick", "JavaScript:OpenWinCenter(700, 800, '', 'EventHocVien.aspx?LevelView=" + LevelIDTemp.Text + "&UserList=" + intUserList.ToString() + "&HocVienID=" + HocVienIDTemp.Text + "&EventCreate=True');");
    //                }
    //                else
    //                {
    //                    hplAddGVTemp.ImageUrl = "";
    //                }
    //                hplUpdateGVTemp.Attributes.Add("OnClick", "JavaScript:OpenWinCenter2(350, 800, '', 'EventHocVien.aspx?LevelView=" + LevelIDTemp.Text + "&TaoHocVien=True&HocVienID=" + HocVienIDTemp.Text + "&EventView=True');");
    //            }
                
    //            Label lblNoidungGVTemp = ((Label)e.Row.FindControl("lblNoidungGV"));
    //            if (lblNoidungGVTemp != null)
    //            {
    //                try
    //                {
    //                    int intHocVienID = Convert.ToInt32(HocVienIDTemp.Text);
    //                    int intLevelID = Convert.ToInt32(LevelIDTemp.Text);
    //                    DataRow dr = null;
    //                    if (intLevelID == 2)
    //                    {
    //                        try
    //                        {
    //                            dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID + 1);
    //                        }
    //                        catch
    //                        {
    //                            dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID);
    //                        }
    //                    }
    //                    else if (intLevelID > 2 && intLevelID <= 6)
    //                    {
    //                        try
    //                        {
    //                            dr = objHocVienLV3LV5.loadHocVienLV3LV5ID(intHocVienID, intLevelID + 1);
    //                        }
    //                        catch
    //                        {
    //                            dr = objHocVienLV3LV5.loadHocVienLV3LV5ID(intHocVienID, intLevelID);
    //                        }

    //                    }
    //                    else if (intLevelID == 1)
    //                    {
    //                        try
    //                        {
    //                            dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID + 1);
    //                        }
    //                        catch
    //                        {
    //                            dr = objHocVienLV2.loadHocVienLV2ID(intHocVienID, intLevelID);
    //                        }
    //                    }
    //                    else if (intLevelID > 6 && intLevelID < 10)
    //                    {
    //                        try
    //                        {
    //                            dr = objHocVienLV6LV8.loadHocVienLV6LV8ID(intHocVienID, intLevelID + 1);
    //                        }
    //                        catch
    //                        {
    //                            dr = objHocVienLV6LV8.loadHocVienLV6LV8ID(intHocVienID, intLevelID);
    //                        }
    //                    }
    //                    if (dr != null)
    //                    {
    //                        if (!dr["NoiDungTaiSao"].ToString().Trim().Equals(""))
    //                        {
    //                            lblNoidungGVTemp.Text = dr["NoiDungTaiSao"].ToString();
    //                        }
    //                        else
    //                        {
    //                            lblNoidungGVTemp.Text = dr["NoiDungNgayGoi"].ToString();
    //                        }
    //                    }
    //                    else
    //                    {
    //                        lblNoidungGVTemp.Text = "";
    //                    }
    //                }
    //                catch
    //                {
    //                    lblNoidungGVTemp.Text = "";
    //                }
    //            }
    //        }
    //    }
    //}

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        loadTimDanhSachHocVien();
    }
    protected void btnInDanhSach_Click(object sender, EventArgs e)
    {
        SaveExcel("DanhSachUngVien_v" + DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + ".xls");
    }
    //protected void grvHocVien_Sorting(object sender, GridViewSortEventArgs e)
    //{
    //    if (ViewState["SortOrder"] == null)
    //    {
    //        ViewState["SortOrder"] = " ASC";
    //    }
    //    else if (ViewState["SortOrder"].ToString() == " ASC")
    //    {
    //        ViewState["SortOrder"] = " DESC";
    //    }
    //    else
    //    {
    //        ViewState["SortOrder"] = " ASC";
    //    }
    //    ViewState["sortExpr"] = e.SortExpression + ViewState["SortOrder"];
    //    loadTimDanhSachHocVien();
    //}
#endregion    
}
